// 路由鉴权，全局守卫
import router from '@/router';

// 引入进度条和样式
//ts-ignore
import NProgress from 'nprogress';
import "nprogress/nprogress.css";

// 引入仓库
import pinia from '@/store';
import useUserStore from "@/store/modules/user";

let userStore = useUserStore(pinia);
// 存储未登录访问白名单
const whiteList = ['/home','/hospital/register','/hospital/detail','/hospital/notice','/hospital/close','/hospital/cancel'];

// 关闭小圆圈
NProgress.configure({ showSpinner: false });

// 添加全局守卫
// 1. 全局前置守卫
router.beforeEach((to, from, next) => {
	// 开始进度条
	NProgress.start();
	// 动态设置网页标题
	document.title = `医院挂号-${to.meta.title}`;


	// 判断是否登录，登录了就放行，没登录就跳转登录页
	let token = userStore.info.token;
	if(token) {
		next();
	} else {
		if(whiteList.includes(to.path)){
			next();
		} else {
			userStore.visible = true;
			next({path: '/home', query: {redirect: to.fullPath}});
		}
	}
});

// 2. 全局后置守卫
router.afterEach((to, from) => {
	// 结束进度条
	NProgress.done();
});